package com.ks.controller;

import com.ks.base.BaseAction;
import com.ks.entity.Exam;
import com.ks.entity.Paper;
import com.ks.entity.Profession;
import com.ks.entity.User;
import com.ks.service.ExamService;
import com.ks.service.PaperService;
import com.ks.service.ProfessionService;
import com.ks.service.UserService;
import com.ks.util.HtmlUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author youniwo
 */
@Controller
@RequestMapping("/exam")
public class ExamMngController extends BaseAction {

    private final static Logger log = Logger.getLogger(ExamMngController.class);

    // Servrice start
    @Autowired(required = false)
    // 自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
    private ExamService<Exam> examService;
    @Autowired
    private PaperService<Paper> paperService;
    @Autowired
    private UserService<User> userService;
    @Autowired
    private ProfessionService<Profession> professionService;
    /**
     * 跳转到考试列表页面
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("toExamList")
    public String toExamList(HttpServletRequest request, HttpServletResponse response) {

        return "pages/examManagement/exam-org-list";
    }

    /**
     * 跳转到添加考试页面
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("toAddExam")
    public ModelAndView toAddExam(HttpServletRequest request, HttpServletResponse response)throws Exception {
        User user = (User) request.getSession().getAttribute("userInfo");
        Map<String, Object> map = examService.toAddExam(user);
        return forword("pages/examManagement/exam-org-add-exam", map);
    }

    /**
     * 创建考试第一步
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("addExamFirstStep")
    @ResponseBody
    public Map<String, Object> addExamFirstStep(HttpServletRequest request, HttpServletResponse response) throws Exception{
        // 判断是不是编辑时进来的
        String examId = request.getParameter("examId");
        String name = request.getParameter("title");
        String starttime = request.getParameter("starttime");
        String duration = request.getParameter("duration");
        String questionrandom = request.getParameter("questionrandom");
        String optionrandom = request.getParameter("optionrandom");
        String address = request.getParameter("address");
        // 考试大类
        String type = request.getParameter("type");
        // 专业
        String pro = request.getParameter("pro");
        // 级别
        String rank = request.getParameter("rank");
        Integer creater = 0;
        Integer unit = 0;
        // 创建人
        User user = (User) request.getSession().getAttribute("userInfo");
        if (null != user) {
            creater = user.getId();
            // 单位
            unit = user.getUnit_id();
        }
        return examService.addExamFirstStep(examId,name, starttime, duration, questionrandom, optionrandom, address, creater,
                unit, type, pro, rank);
    }
    /**
     * 展示试卷列表
     *
     * @param response
     * @throws Exception
     */
    @RequestMapping("/paperList")
    public void paperList(Exam exam,HttpServletRequest request, HttpServletResponse response) throws Exception {

        HtmlUtil.writerJson(response, examService.paperList(exam));
    }

    @RequestMapping("examList")
    public void examList(HttpServletResponse response) throws Exception {

        HtmlUtil.writerJson(response, examService.examList());
    }

    /**
     * 创建考试第二步
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("addExamSecondStep")
    @ResponseBody
    public Map<String, Object> addExamSecondStep(HttpServletRequest request, HttpServletResponse response) {
        String paperId = request.getParameter("paperId");
        String examId = request.getParameter("examId");
        return examService.addExamSecondStep(paperId, examId);
    }

    /**
     * 创建考试第三步
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("addExamThirdStep")
    @ResponseBody
    public Map<String, Object> addExamThirdStep(HttpServletRequest request, HttpServletResponse response) {
        String studentIds = request.getParameter("studentIds");
        String examId = request.getParameter("examId");
        String paperId = request.getParameter("paperId");

        return examService.addExamThirdStep(studentIds, examId, paperId);

    }

    /**
     * 创建考试第四步
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("addExamFourthStep")
    @ResponseBody
    public Map<String, Object> addExamFourthStep(HttpServletRequest request, HttpServletResponse response) {
        String examId = request.getParameter("examId");
        return examService.addExamFourthStep(examId);
    }

    /**
     * 跳转到考试详情页面
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("toExamDetail")
    public String toExamDetail(Model model, HttpServletRequest request, HttpServletResponse response) {
        String examId = request.getParameter("examId");
        examService.toExamDetail(model, examId);
        return "pages/examManagement/exam-detail";
    }

    /**
     * 启用考试
     *
     * @param request
     * @param response
     */
    @RequestMapping("openExam")
    @ResponseBody
    public Map<String, Object> openExam(HttpServletRequest request, HttpServletResponse response) {
        String examId = request.getParameter("examId");
        return examService.openExam(examId);
    }

    /**
     * 编辑考试
     *
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("editExam")
    public ModelAndView editExam(Model model, HttpServletRequest request, HttpServletResponse response) {
        String examId = request.getParameter("examId");
        Map<String, Object> map = examService.editExam(model, examId, request);
        return forword("pages/examManagement/exam-org-add-exam", map);

    }

    /**
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("copyExam")
    public void copyExam(Model model, HttpServletRequest request, HttpServletResponse response) {
        String examId = request.getParameter("examId");
        examService.copyExam(examId);
        sendSuccessMessage(response, "复制成功~");
    }

    /**
     * 删除考试
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("delExam")
    @ResponseBody
    public Map<String, Object> delExam(HttpServletRequest request, HttpServletResponse response) {
        String examId = request.getParameter("examId");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Integer status = 1;
        String msg = "删除成功！";
        try {
            examService.del(examId);
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            status = 0;
            msg = e.getMessage();
        }
        resultMap.put("status", status);
        resultMap.put("msg", msg);
        return resultMap;
    }

    /**
     * 查询部门人数和部门下的考生
     * @param request
     */
    @RequestMapping("findUser")
    @ResponseBody
    public List<User> findUser(HttpServletRequest request){
        String rank = request.getParameter("rankId");
        String pro = request.getParameter("proId");
        String unitId = request.getParameter("unitId");
        Map<String,Object> map = new HashMap<>();
        map.put("rankId",rank);
        map.put("proId",pro);
        map.put("unitId",unitId);
        return examService.finUser(map);
    }

    /**
     *
     */
    @RequestMapping("findByUser")
    @ResponseBody
    public void findByUser(HttpServletRequest request){
        String rankId = request.getParameter("rankId");
        String proId = request.getParameter("proId");
        String unitId = request.getParameter("unitId");
        Map<String,Object> map = new HashMap<>();
        map.put("rankId",rankId);
        map.put("proId",proId);
        map.put("unitId",unitId);
    }

    /**
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/printPage")
    public ModelAndView printPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String examId = request.getParameter("examId");
        String userid = request.getParameter("userid");
        //查询考试下的人员
        Map<String, Object> context = getRootMap();
        context = examService.findUserByExamId(userid,examId);
        context.put("examId",examId);
        return forword("/pages/examManagement/admission", context);

    }

    /**
     *
     * 关联ajax
     *
     */

    @RequestMapping("typeAjax")
    @ResponseBody
    public List<Profession> typeAjax(String typeId){
        return professionService.findByTypeId(typeId);
    }

    /**
     * 发布考试页面试卷预览
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("questionsView")
    @ResponseBody
    public Map<String, Object> questionsView(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String paperId = request.getParameter("paperId");
        String path = "/examsys/";
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
        return examService.questionsView(paperId,basePath);
    }
}
